
#ifndef RTDEAPI_H
#define RTDEAPI_H

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "rtdebasedefine.h"
#include "cdrtdebasedefine.h"
#include "rtdeutils.h"
#include "rtde_hash.h"

#define RTDE_IP_MAX_NUM 10


typedef struct
{
    RtdeHandle rtdeHandle;
    
    int output_vars_num;
    int input_vars_num;
    int methods_num;
    
    rtde_varlist_t output_vars[RTDE_MAX_OUTPUT_VAR];
    rtde_varlist_t input_vars[RTDE_MAX_INPUT_VAR];
    rtde_method_list_t methods_list[RTDE_MAX_METHODS];

    uint8_t rtde_recv_state;
    uint8_t rtde_recvbuf[RTDE_SOCK_BUF_MAX];

    uint8_t rtde_ioctrl_recvbuf[RTDE_SOCK_BUF_MAX];
    uint8_t rtde_ioctrl_sendbuf[RTDE_SOCK_BUF_MAX];

    rtde_data_t output_data_package[RTDE_MAX_OUTPUT_VAR];
    rtde_data_t input_data_package[RTDE_MAX_INPUT_VAR];

    Socket_fd_t Socket_fd;

    // 全局哈希表
    input_hash_t  *g_inputs_hash;
    output_hash_t *g_outputs_hash;
    method_hash_t *g_methods_hash;
    // 内存池
    input_hash_t  input_hash_pool[MAX_INPUT_HASH];
    output_hash_t output_hash_pool[MAX_OUTPUT_HASH];
    method_hash_t method_hash_pool[MAX_METHOD_HASH];
    // 内存池索引
    int input_pool_idx;
    int output_pool_idx;
    int method_pool_idx;

    pthread_t th_receive;  //接收数据线程
    

}RtdeDatespace_t;//每个ip下所有操作所需要的数据空间

typedef struct
{   
    RtdeHandle rtdeHandle;
    RtdeDatespace_t *rtde_dataspace;
    uint8_t RtdeIp[20];
    uint8_t RtdespaceNum;
}RtdeHandleData_t;//一个IP一个RtdeHandleData_t

//获取SDK版本号
RtdeResult rtde_get_SDK_Version(uint8_t* version);

//创建RtdeHandle
RtdeResult rtde_create_handle(RtdeHandle *rtdeHandle,const char *ip);

//获取RtdeHandle对应的RtdeDatespace
RtdeDatespace_t* get_rtde_dataspace(RtdeHandle rtdeHandle);

//三个端口创建
RtdeResult rtde_create(RtdeHandle rtdeHandle,const char *ip);

//请求控制器使协议version生效
//result 0-success，非0-错误码
RtdeResult rtde_protocol_version_request(RtdeHandle rtdeHandle,int64_t version);

//获取控制器所有输出变量
RtdeResult rtde_output_variables_get(RtdeHandle rtdeHandle,int offset,RtdeVar *rtdeVar);

//获取控制器所有输出入量
RtdeResult rtde_input_variables_get(RtdeHandle rtdeHandle,int offset,RtdeVar *rtdeVar);

//获取控制器所有方法
RtdeResult rtde_control_methods_get(RtdeHandle rtdeHandle,int offset,RtdeFunc *rtdeFunc);

//获取控制器所有配方
RtdeResult rtde_all_recipe_get(RtdeHandle rtdeHandle);

//关闭连接
RtdeResult rtde_destroy(RtdeHandle rtdeHandle);

//发送输入配方到控制器
//需要根据数据类型检查后期返回数据的总字节数不能超过1400-4字节
RtdeResult rtde_input_setup_send(RtdeHandle rtdeHandle,uint16_t number,uint32_t *varId, uint32_t *recipeId);

//发送输出配方到控制器
RtdeResult rtde_output_setup_send(RtdeHandle rtdeHandle,uint16_t number,uint32_t *varId, int frequency, uint32_t *recipeId);

//开始
RtdeResult rtde_output_start(RtdeHandle rtdeHandle,uint32_t recipeId);

//暂停
RtdeResult rtde_output_pause(RtdeHandle rtdeHandle,uint32_t recipeId);

//接收数据
RtdeResult rtde_ouput_data_receive(RtdeHandle rtdeHandle,uint32_t recipeId,void *data);

//开始透传
RtdeResult crtde_servoj(RtdeHandle rtdeHandle,int methodCode,double cutoffFreq, double jointPose[6],uint8_t exjNum,double *exjPose);

//停止透传
RtdeResult crtde_servo_stop(RtdeHandle rtdeHandle,int methodCode,double deceleration);

//设置速度百分比
RtdeResult rtde_set_percentvelocity(RtdeHandle rtdeHandle,uint16_t percenVelocity);

//重置错误状态
RtdeResult crtde_resterror(RtdeHandle rtdeHandle,int methodCode);

#endif /* RTDEAPI_H */
